package huichun_r

import (
	"fmt"
	"frozen-go-mini/common/domain"
	"frozen-go-mini/common/mycontext"
	"frozen-go-mini/domain/huichun_m"
	"frozen-go-mini/req"
	"frozen-go-mini/resp"
	"github.com/gin-gonic/gin"
	"strings"
)

// @Tags 挥春
// @Summary 我的足迹
// @Param Authorization header string true "token"
// @Success 200 {object} []Product
// @Router /v1.0/huichun/mine/footprint [get]
func MineFootprint(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	model := domain.CreateModelContext(myCtx)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
	product := huichun_m.GetMyFootprint(model, userId)
	myCollect := huichun_m.GetIsMyCollect(model, userId)
	var response []Product
	for _, p := range product {
		response = append(response, Product{
			Id:        p.ID,
			Name:      p.Sku,
			Desc:      p.Desc,
			Pic:       p.Pics,
			Tags:      strings.Split(p.Tags, ","),
			Price:     fmt.Sprintf("%.2f", float64(p.DiscPrice)/100),
			Sales:     p.Sales,
			Stock:     p.Stock,
			IsCollect: myCollect[p.ID],
		})
	}

	resp.ResponseOk(c, response)
	return myCtx, nil
}

type MineDoCollectReq struct {
	ProductId uint64 `json:"product_id"` // 商品id
	Collect   bool   `json:"collect"`    // 是否收藏
}

// @Tags 挥春
// @Summary 收藏/取消收藏
// @Param userId header uint64 true "token"
// @Param MineDoCollectReq body MineDoCollectReq true "code"
// @Success 200
// @Router /v1.0/huichun/mine/collect [post]
func MineDoCollect(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	model := domain.CreateModelContext(myCtx)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
	var param MineDoCollectReq
	if err := c.ShouldBindJSON(&param); err != nil {
		return myCtx, err
	}
	if err := huichun_m.AddUserCollect(model, huichun_m.UserCollect{
		UserId:    userId,
		ProductId: param.ProductId,
		Collect:   param.Collect,
	}); err != nil {
		return myCtx, err
	}
	resp.ResponseOk(c, "")
	return myCtx, nil
}

// @Tags 挥春
// @Summary 我的收藏
// @Param userId header uint64 true "token"
// @Success 200 {object} []Product
// @Router /v1.0/huichun/mine/collect [get]
func MyCollect(c *gin.Context) (*mycontext.MyContext, error) {
	myCtx := mycontext.CreateMyContext(c.Keys)
	model := domain.CreateModelContext(myCtx)
	userId, err := req.GetUserId(c)
	if err != nil {
		return myCtx, err
	}
	product := huichun_m.GetMyCollect(model, userId)
	var response []Product
	for _, p := range product {
		response = append(response, Product{
			Id:        p.ID,
			Name:      p.Sku,
			Desc:      p.Desc,
			Pic:       p.Pics,
			Tags:      strings.Split(p.Tags, ","),
			Price:     fmt.Sprintf("%.2f", float64(p.DiscPrice)/100),
			Sales:     p.Sales,
			Stock:     p.Stock,
			IsCollect: true,
		})
	}

	resp.ResponseOk(c, response)
	return myCtx, nil
}
